package com.baizhi.service.impl;

import com.baizhi.dao.ProductDao;
import com.baizhi.entity.Product;
import com.baizhi.service.ProductService;
import com.baizhi.util.MyBatisUtils;
import org.apache.ibatis.session.SqlSession;

import java.util.List;

public class ProductServiceImpl implements ProductService {
    @Override
    public List<Product> showProducts(Integer pageNum, Integer pageSize) {
        SqlSession sqlSession = MyBatisUtils.openSession();
        List<Product> products = null;
        try{
            ProductDao productMapper = sqlSession.getMapper(ProductDao.class);
            products = productMapper.selectProducts(pageNum, pageSize);
            sqlSession.commit();
        }catch (Exception e){
            sqlSession.rollback();
            e.printStackTrace();
            throw new RuntimeException(e);
        }finally {
            MyBatisUtils.close(sqlSession);
        }
        return products;
    }

    @Override
    public Product showProductById(Integer id) {
        SqlSession sqlSession = MyBatisUtils.openSession();
        Product product = null;
        try{
            ProductDao productMapper = sqlSession.getMapper(ProductDao.class);
            product = productMapper.selectProductById(id);
            sqlSession.commit();
        }catch (Exception e){
            sqlSession.rollback();
            e.printStackTrace();
            throw new RuntimeException(e);
        }finally {
            MyBatisUtils.close(sqlSession);
        }
        return product;
    }

    @Override
    public void addProduct(Product b) {
        SqlSession sqlSession = MyBatisUtils.openSession();
        try{
            ProductDao bookMapper = sqlSession.getMapper(ProductDao.class);
            bookMapper.insertProduct(b);
            sqlSession.commit();
        }catch (Exception e){
            sqlSession.rollback();
            e.printStackTrace();
            throw new RuntimeException(e);
        }finally {
            MyBatisUtils.close(sqlSession);
        }
    }

    @Override
    public void removeProductById(Integer id) {
        SqlSession sqlSession = MyBatisUtils.openSession();
        try{
            ProductDao bookMapper = sqlSession.getMapper(ProductDao.class);
            bookMapper.deleteProductById(id);
            sqlSession.commit();
        }catch (Exception e){
            sqlSession.rollback();
            e.printStackTrace();
            throw new RuntimeException(e);
        }finally {
            MyBatisUtils.close(sqlSession);
        }
    }

    @Override
    public void updateProduct(Product b) {
        SqlSession sqlSession = MyBatisUtils.openSession();
        try{
            ProductDao bookMapper = sqlSession.getMapper(ProductDao.class);
            bookMapper.updateProduct(b);
            sqlSession.commit();
        }catch (Exception e){
            sqlSession.rollback();
            e.printStackTrace();
            throw new RuntimeException(e);
        }finally {
            MyBatisUtils.close(sqlSession);
        }
    }
}
